var register = {
	//验证码
	getCaptcha:function(){
		var windowUrl = window.URL || window.webkitURL;//处理浏览器兼容性
		  var xhr = new XMLHttpRequest();
		  var url = "/api/captcha";//验证码请求地址
		  xhr.open("GET", url, true);
		  xhr.responseType = "blob";
		  xhr.onload = function () {
		    if (this.status == 200) {
		      var blob = this.response;
		      $("#captcha").attr("src", windowUrl.createObjectURL(blob));
		    }
		  }
		  xhr.send();
	},
	emailIsExiting:function(email){
		var flag = false;
		$.ajax({ 
			type:"POST", 
			url:'/api/emailIsExist',
			data:{
				"email":email,
			},
			async:false,
			success:function(res){
				flag = res;
			},
			error:function(XMLHttpRequest, textStatus){ 

			}
		});
		return flag;
	},
	//注册
	regis:function(){
		var userName = $(".pregister-form input[name='userName']").val();
		if(userName == ''){
			layer.msg("Please input userName ！");
			return;
		}
		if(!validate.username(userName)){
			layer.msg("Username only 4-16 character alphanumeric underscores/minus are allowed ！");
			return;
		}
		
		var interests = $(".pregister-form input[name='interests']").val();
		if(interests == ''){
			layer.msg("Please input interests !");
			return;
		}
		if(!validate.strLength(interests)){
			layer.msg("A maximum of 40 characters is allowed ！");
			return;
		}
		var org = $(".pregister-form input[name='company']").val();
		if(org == ''){
			layer.msg("Please input company/Institution !");
			return;
		}
		if(!validate.strLength(org)){
			layer.msg("A maximum of 40 characters is allowed ！");
			return;
		}
		var page = $(".pregister-form input[name='page']").val();
		if(page == ''){
			layer.msg("Please input page !");
			return;
		}
		if(!validate.strLength(page)){
			layer.msg("A maximum of 40 characters is allowed ！");
			return;
		}
		var email = $(".pregister-form input[name='email']").val();
		if(email == ''){
			layer.msg("Please input email !");
			return;
		}
		if(!validate.email(email)){
			layer.msg("email is nonlicet ！");
			return;
		}
		//检测是否存在
		if(register.emailIsExiting(email)){
			layer.msg("email already exists ！");
			return;
		}
		var password = $(".pregister-form input[name='password']").val();
		if(password == ''){
			layer.msg("Please input password !");
			return;
		}
		if(!validate.pssword(password)){
			layer.msg("password is nonlicet ！At least 6-12 characters, only letters and numbers.");
			return;
		}
		var passwordCondfirm = $(".pregister-form input[name='passwordCondfirm']").val();
		/*if(passwordCondfirm == ''){
			layer.msg("Please input Condfirm password !");
			return;
		}
		if(!validate.pssword(passwordCondfirm)){
			layer.msg("passwordCondfirm is nonlicet ！At least 6-12 characters,includeing one number/one uppercse letter/one lowerercase letter.");
			return;
		}*/
		if(password != passwordCondfirm){
			layer.msg("password inconsistency !");
			return;
		}
		
		var $sigdiv = $("#signature")
		var datapair = $sigdiv.jSignature("getData", "image") 
		if(datapair[1] == 'iVBORw0KGgoAAAANSUhEUgAAAcoAAADICAYAAABoI6FjAAAAAXNSR0IArs4c6QAACL9JREFUeF7t17ENwDAMBLF4/6UzgV1c+0yvQpSBQ87nI0CAAAECBK4Chw0BAgQIECBwFxBKr4MAAQIECDwEhNLzIECAAAECQukNECBAgACBJuCPsrmZIkCAAIERAaEcObQ1CRAgQKAJCGVzM0WAAAECIwJCOXJoaxIgQIBAExDK5maKAAECBEYEhHLk0NYkQIAAgSYglM3NFAECBAiMCAjlyKGtSYAAAQJNQCibmykCBAgQGBEQypFDW5MAAQIEmoBQNjdTBAgQIDAiIJQjh7YmAQIECDQBoWxupggQIEBgREAoRw5tTQIECBBoAkLZ3EwRIECAwIiAUI4c2poECBAg0ASEsrmZIkCAAIERAaEcObQ1CRAgQKAJCGVzM0WAAAECIwJCOXJoaxIgQIBAExDK5maKAAECBEYEhHLk0NYkQIAAgSYglM3NFAECBAiMCAjlyKGtSYAAAQJNQCibmykCBAgQGBEQypFDW5MAAQIEmoBQNjdTBAgQIDAiIJQjh7YmAQIECDQBoWxupggQIEBgREAoRw5tTQIECBBoAkLZ3EwRIECAwIiAUI4c2poECBAg0ASEsrmZIkCAAIERAaEcObQ1CRAgQKAJCGVzM0WAAAECIwJCOXJoaxIgQIBAExDK5maKAAECBEYEhHLk0NYkQIAAgSYglM3NFAECBAiMCAjlyKGtSYAAAQJNQCibmykCBAgQGBEQypFDW5MAAQIEmoBQNjdTBAgQIDAiIJQjh7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cObQ1CRAgQKAJCGVzM0WAAAECIwI/6GUAycc0Z9sAAAAASUVORK5CYII='){
			layer.msg("Please sign !");
			return;
		}
		
		var captcha = $(".pregister-form input[name='captcha']").val();
		if(captcha == ''){
			layer.msg("Please input verification code !");
			return;
		}
		if(!validate.isFourDigitNumber(captcha)){
			layer.msg("It is not four characters !");
			return;
		}
		
		//数据校验
		var config = {
				url:'/api/regis',
				data:{
					"v":captcha,
					"u":userName,
					"p":passwordCondfirm,
					"email":email,
					"page":page,
					"org":org,
					"interests":interests,
					"datapair":datapair[1]
				},
				callBackFunc:function(data){
					if(data == '1'){
						layer.msg("Verification code error");
					}
					if(data == '0'){
						layer.msg("registered successfully");
						//跳转至登录页
						window.location.href = '/login';
					}
				},
				callBackErrorFunc:function(){}
		};
		HD.doPost(config);//提交
	}
}

$(function(){
	//页面默认加载时请求验证图片
	register.getCaptcha();
	//验证码图片点击时刷新
	$("#captcha").click(function(){
		register.getCaptcha();
	});
	//注册提交
	$(".pregister-sub").click(function(){
		register.regis();
	})
})